package com.titan.core.utils.file;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.Writer;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.io.IOUtils;

import com.opencsv.CSVReader;
import com.opencsv.CSVWriter;
import com.titan.core.utils.comm.SocketUtils;

/**
 * @author 周成功
 * @email 86898837@qq.com
 * @date 2020年02月20日
 */
public class CSVUtils {
    private static final char DEFAULT_SEPARATOR = ',';  // 默认字段分隔符
    private static final char DEFAULT_QUOTE_CHARACTER = '"';    // 字段包装符号
//    private static final char DEFAULT_QUOTE_CHARACTER = '\u0000';    // 字段包装符号
    private static final char DEFAULT_ESCAPE_CHARACTER = '\\';   // 转移字符
    private static final String DEFAULT_LINE_END = "\n";
    private static final String CHARSET = "GBK";
    
    
    public static void main(String[] args) {
        try {
            String file = "c:\\temp\\temp.csv";
            List<String[]> lines = new ArrayList<String[]>();
            lines.add(new String[]{"\"a1\"", "b\"'\\1", "c1"});
            CSVUtils.write(file, lines);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
        
    public static List<String[]> read(String file) throws IOException {
        InputStream input = null;
        CSVReader csv = null;
        Reader reader = null;
        try {
            File csvFile = new File(file);
            input = new FileInputStream(csvFile);    // 设置覆盖方式
            reader = new InputStreamReader(input, CHARSET); // 设置字符集
            csv = new CSVReader(reader, DEFAULT_SEPARATOR, DEFAULT_QUOTE_CHARACTER, DEFAULT_ESCAPE_CHARACTER);
            return csv.readAll(); 
        } finally {
            close(csv);
        }
    }
    
    public static void write(String file, List<String[]> lines) throws IOException {
        OutputStream output = null;
        CSVWriter csv = null;
        Writer writer = null;
        try {
            File csvFile = new File(file);
            if (!csvFile.exists()) csvFile.createNewFile();
            output = new FileOutputStream(csvFile, false);    // 设置覆盖方式
            writer = new OutputStreamWriter(output, CHARSET); // 设置字符集
            csv = new CSVWriter(writer, DEFAULT_SEPARATOR, DEFAULT_QUOTE_CHARACTER, DEFAULT_ESCAPE_CHARACTER, DEFAULT_LINE_END);
            csv.writeAll(lines);
            csv.flush();
        } finally {
            close(csv);
            SocketUtils.closeQuietly(writer);
            SocketUtils.closeQuietly(output);
        }
    }   
	
	
	public static void close(CSVWriter writer) {
		if(writer!=null) {
			try {
				writer.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
	
    public static void close(CSVReader reader) {
        if(reader!=null) {
            try {
                reader.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
